package com.hiflying.smartlink.v8;

import android.content.Context;
import android.util.Log;

import com.accloud.utils.LocalUtils;
import com.hiflying.smartlink.ISmartLinker;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class AirkissSendAction implements Runnable {
    private AirkissProtocol airkissProtocol;
    private Context mContext;
    private String mOthers;
    private String mPassword;
    private ISmartLinker mSmartLinker;
    private String mSsid;
    private int port = 47777;
    private DatagramSocket udpSocket;

    public AirkissSendAction(Context mContext2, ISmartLinker mSmartLinker2, String mSsid2, String mPassword2, String mOthers2) {
        this.mContext = mContext2;
        this.mSmartLinker = mSmartLinker2;
        this.mSsid = mSsid2;
        this.mPassword = mPassword2;
        this.mOthers = mOthers2;
    }

    private void send(int len) {
        if (len != 0) {
            try {
                Log.v("AirkissSendAction", "Send:" + len + String.format(", 0x%02X", Integer.valueOf(len)));
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        } else {
            len = 8;
            Log.v("AirkissSendAction", "Send (0->8):8" + String.format(", 0x%02X", 8));
        }
        byte[] b = new byte[len];
        Arrays.fill(b, (byte) -86);
        try {
            this.udpSocket.send(new DatagramPacket(b, len, InetAddress.getByName(LocalUtils.HOST_BROADCAST), this.port));
        } catch (Exception e) {

        }
//
    }

    @Override // java.lang.Runnable
    public void run() {
        this.airkissProtocol = new AirkissProtocol(this.mSsid, this.mPassword, this.mOthers);
        try {
            this.udpSocket = new DatagramSocket();
            int i = 0;
            while (this.mSmartLinker.isSmartLinking()) {
                int[] bSend = this.airkissProtocol.getByte(i);
                if (bSend == null) {
                    i = -1;
                } else {
                    for (int i2 : bSend) {
                        send(i2);
                        msleep(5);
                    }
                }
                msleep(50);
                i++;
            }
            if (this.udpSocket != null) {
                this.udpSocket.close();
                this.udpSocket.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (this.udpSocket != null) {
                this.udpSocket.close();
                this.udpSocket.disconnect();
            }
        } catch (Throwable th) {
            if (this.udpSocket != null) {
                this.udpSocket.close();
                this.udpSocket.disconnect();
            }
            throw th;
        }
    }

    private void msleep(int ms) {
        try {
            Thread.sleep((long) ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
